package com.feiyue.erp.api.controller;

import com.daiyilu.common.Result;
import com.daiyilu.common.SmsUtil;
import com.feiyue.erp.service.RedisService;
import com.feiyue.erp.service.LoginService;
import com.feiyue.erp.vo.CustomerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

import static java.lang.Thread.*;

@RestController
@RequestMapping(value = "/api")
@Api(value="登录相关接口")
public class LoginController {
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    LoginService loginSevice;
    @Autowired
    RedisService redisService;

    @RequestMapping(value = "/thread/{name}")
    public Result testThreadLocal(@PathVariable("name") String name) {
        //@PathVariable String name
        System.out.println("线程开始名:"+Thread.currentThread().getName()+ "| name="+name);
        try {
            sleep((long) ( new Random().nextInt(50)));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Result result = new Result();
        result.setCode(0);
        result.setMsg("成2功!");
        return result;
    }

    @RequestMapping(value = "/login",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value="登录接口", notes="登录接口" ,httpMethod="POST")
    public Result login() {
        Result result = new Result();
        result.setCode(0);
        result.setMsg("成2功!");
        CustomerVO customerVO = new CustomerVO();
        customerVO.setUserName("liaoliang");
        result.setData(customerVO);
        return result;
    }

    @RequestMapping(value = "/findUser")
    @ApiOperation(value="查找用户接口", notes="查找用户接口" ,httpMethod="POST")
    public Result findUser() {
        loginSevice.findLoginUser();
        Result result = new Result();
        result.setCode(0);
        result.setMsg("成功!");
        CustomerVO customerVO = new CustomerVO();
        customerVO.setUserName("liaoliang");
        result.setData(customerVO);
        return result;
    }

    @RequestMapping(value = "/sms")
    @ApiOperation(value="短信测试接口", notes="短信测试接口" ,httpMethod="POST")
    public Result sms() {
        SmsUtil.sendSmsByLuosimao("18571475594","验证码111111，您正登录仓海优选商城，如非本人操作请联系客服。【仓海优选】");
        Result result = new Result();
        result.setCode(0);
        result.setMsg("成2功!");
        CustomerVO customerVO = new CustomerVO();
        customerVO.setUserName("liaoliang");
        result.setData(customerVO);
        return result;
    }

    @RequestMapping(value = "/redis")
    @ApiOperation(value="redis测试接口", notes="redis测试接口" ,httpMethod="POST")
    public Result redis() {
        logger.info("11111111111111111111111111");
        redisService.set("AAAAAAAAAAAA","liao");

        Result result = new Result();
        result.setCode(0);
        result.setMsg("成2功!");
        CustomerVO customerVO = new CustomerVO();
        customerVO.setUserName("liaoliang");
        CustomerVO vo = (CustomerVO)redisService.get("22222");

        List<Map<CustomerVO, List<CustomerVO>>> listMap = new ArrayList<>();
        Map<CustomerVO, List<CustomerVO>> map = new HashMap<>();
        List<CustomerVO> list = new ArrayList<>();
        list.add(customerVO);
        map.put(customerVO,list);
        listMap.add(map);
        result.setData(listMap);
        return result;
    }
}